package com.ihisun.tmhcms.core.service.projectservice.impl;

import com.ihisun.tmhcms.core.dao.projectdao.EditProjectBannerDao;
import com.ihisun.tmhcms.core.dao.projectdao.EditProjectDao;
import com.ihisun.tmhcms.core.dto.projectdto.EditProjectBannerDto;
import com.ihisun.tmhcms.core.dto.projectdto.EditProjectDto;
import com.ihisun.tmhcms.core.service.projectservice.EditProjectService;
import com.ihisun.tmhcms.core.vo.ProjectVo;
import com.ihisun.tmhcms.utils.DateUtils;
import com.ihisun.tmhcms.utils.PageBean;
import com.ihisun.tmhcms.utils.ResponseVo;
import com.ihisun.tmhcms.utils.UuIdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.math.BigInteger;
import java.util.List;

@Slf4j
@Service
public class EditProjectServiceImpl implements EditProjectService {
    @Resource
    EditProjectDao editProjectDao;
    @Resource
    EditProjectBannerDao editProjectBannerDao;
    @PersistenceContext
    EntityManager entityManager;

    @Override
    public void deleteBydoctorId(String doctorId) {
        editProjectDao.deleteByDoctorId(doctorId);
    }

    @Transactional
    @Override
    public ResponseVo editProject(EditProjectDto editProjectDto) {

        if (editProjectDto.getProjectId() != null && !"".equals(editProjectDto.getProjectId())) {
            //修改
            /**
             *修改项目主体信息
             */
            editProjectDto.setUpdateTime(DateUtils.getCurrentTime());
            editProjectDao.save(editProjectDto);
            /**
             *
             * 删除项目轮播
             *
             */
            editProjectBannerDao.deleteByProjectId(editProjectDto.getProjectId());
            /**
             *
             * 保存新轮播图
             *
             */
            List<EditProjectBannerDto> editProjectBannerDtos = editProjectDto.getEditProjectBannerDtos();
            for (int i = 0; i < editProjectBannerDtos.size(); i++) {
                editProjectBannerDtos.get(i).setCreateTime(DateUtils.getCurrentTime());
                editProjectBannerDtos.get(i).setUpdateTime(DateUtils.getCurrentTime());
                editProjectBannerDtos.get(i).setProjectId(editProjectDto.getProjectId());
                editProjectBannerDao.save(editProjectBannerDtos.get(i));
            }
        } else {
            String uuid = UuIdUtil.getToUuid();
            //新增
            /**
             *
             *
             * 保存项目基本信息
             *
             *
             */
            editProjectDto.setUpdateTime(DateUtils.getCurrentTime());
            editProjectDto.setCreateTime(DateUtils.getCurrentTime());
            editProjectDto.setProjectId(uuid);
            editProjectDao.save(editProjectDto);
            /**
             * 保存轮播
             */
            List<EditProjectBannerDto> editProjectBannerDtos = editProjectDto.getEditProjectBannerDtos();
            for (int i = 0; i < editProjectBannerDtos.size(); i++) {
                editProjectBannerDtos.get(i).setCreateTime(DateUtils.getCurrentTime());
                editProjectBannerDtos.get(i).setUpdateTime(DateUtils.getCurrentTime());
                editProjectBannerDtos.get(i).setProjectId(uuid);
                editProjectBannerDao.save(editProjectBannerDtos.get(i));
            }
        }


        return ResponseVo.ok();
    }

    @Override
    public ResponseVo findByProjectId(String projectId, Integer valueState) {
        EditProjectDto byProjectId = editProjectDao.findByProjectId(projectId);
        byProjectId.setState(valueState);
        EditProjectDto save = editProjectDao.save(byProjectId);
        return ResponseVo.ok();
    }

    @Override
    public ResponseVo searchProject(String name, String typeName, Integer state, Integer pageSize, Integer pageNum) {
        StringBuilder seleteSql = new StringBuilder();

        seleteSql.append("select * from p_project pp INNER JOIN d_doctor dd INNER JOIN p_project_class ppc on dd.doctor_id=pp.doctor_id and pp.project_type_id=ppc.project_type_id and pp.state!=3");
        if (name != null && !"".equals(name)) {
            seleteSql.append(" or dd.doctor_name like concat('%','" + name + "','%') or pp.project_name LIKE concat('%','" + name + "','%')");
        }
        if (typeName != null && !"".equals(typeName)) {
            seleteSql.append(" or ppc.project_class_name='" + typeName + "'");
        }
        if (state != null) {
            seleteSql.append(" or pp.state='" + state + "'");
        }
        seleteSql.append("  GROUP BY pp.project_id ORDER BY sales_volume desc limit " + (pageNum - 1) * 10 + ", " + pageSize + "");
        List<ProjectVo> resultList = entityManager.createNativeQuery(seleteSql.toString(), ProjectVo.class).getResultList();
        StringBuilder contSql = new StringBuilder();
        contSql.append("select count(*) from p_project pp INNER JOIN d_doctor dd INNER JOIN p_project_class ppc on dd.doctor_id=pp.doctor_id and pp.project_type_id=ppc.project_type_id and pp.state!=3");
        if (name != null && !"".equals(name)) {
            contSql.append(" or dd.doctor_name LIKE concat('%','" + name + "','%') or pp.project_name LIKE concat('%','" + name + "','%')");
        }
        if (typeName != null && !"".equals(typeName)) {
            contSql.append(" or ppc.project_class_name='" + typeName + "' ");
        }
        if (state != null) {
            contSql.append(" or pp.state='" + state + "'");
        }
        contSql.append("  GROUP BY pp.project_id ORDER BY sales_volume desc ");

        log.info("sql=" + seleteSql.toString());
        log.info("count sql:" + contSql.toString());

        BigInteger cont = (BigInteger) entityManager.createNativeQuery(contSql.toString()).getSingleResult();
        log.info("返回结果集：" + resultList);
        PageBean page = new PageBean(pageSize, cont.intValue(), pageNum, resultList);
        return ResponseVo.ok(page);
    }

}
